package com.nfu.zlsp.dev.controller;

import com.nfu.zlsp.dev.common.utils.PageUtils;
import com.nfu.zlsp.dev.common.utils.R;
import com.nfu.zlsp.dev.entity.AddressEntity;
import com.nfu.zlsp.dev.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 
 *
 * @author txh
 * @email hunterapeman@gmail.com
 * @date 2023-03-12 01:54:16
 */
@RestController
@RequestMapping("dev/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 列表
     */
    @RequestMapping("/test")
    //@RequiresPermissions("dev:address:list")
    public R test(@RequestParam Map<String, Object> params){
        System.out.println("hhhhhhhhhhhhhhhhhhhhhhh");
        System.out.println("hhhhhhhhhhhhhhhhhhhhhhh");
        System.out.println("hhhhhhhhhhhhhhhhhhhhhhh");

        return R.ok().put("test", "testsetsetsetsete");
    }

    /**
     * 根据address_areaId获取地址信息-ajax
     */
    @RequestMapping(value = "getAddress/{areaId}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    protected String getAddressByAreaId(@PathVariable String areaId) {
        JSONObject object = new JSONObject();
        System.out.println("获取AreaId为{}的地址信息:" + areaId);
        List<AddressEntity> addressList = addressService.getList(null, areaId);
        if (addressList == null || addressList.size() <= 0) {
            object.put("success", false);
            return object.toJSONString();
        }
        System.out.println("获取该地址可能的子地址信息");
        List<AddressEntity> childAddressList = addressService.getList(null, addressList.get(0).getAddressAreaid());
        object.put("success", true);
        object.put("addressList", addressList);
        object.put("childAddressList", childAddressList);
        return object.toJSONString();
    }




    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("dev:address:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = addressService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{addressAreaid}")
    //@RequiresPermissions("dev:address:info")
    public R info(@PathVariable("addressAreaid") String addressAreaid){
		AddressEntity address = addressService.getById(addressAreaid);

        return R.ok().put("address", address);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("dev:address:save")
    public R save(@RequestBody AddressEntity address){
		addressService.save(address);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("dev:address:update")
    public R update(@RequestBody AddressEntity address){
		addressService.updateById(address);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("dev:address:delete")
    public R delete(@RequestBody String[] addressAreaids){
		addressService.removeByIds(Arrays.asList(addressAreaids));

        return R.ok();
    }

}
